$(function () {
    //计算总价
    var array = $(".qprice");
    var totalCost = 0;
    for (var i = 0; i < array.length; i++) {
        var val = parseFloat($(".qprice").eq(i).html().substring(1));
        totalCost += val;
    }

    // console.log("array"+array.length);
    $("#totalprice").html("￥" + totalCost);

    /*    $(".add").click(function(){
            var t=$(this).parent().find('input[class*=text_box]');
            t.val(parseInt(t.val())+1)
        });
        $(".min").click(function(){
            var t=$(this).parent().find('input[class*=text_box]');
            t.val(parseInt(t.val())-1)
            if(parseInt(t.val())<0){
                t.val(0);
            }
        });*/
    $
    (".btn-go").click(function () {
        var array = $(".productId");
        var arrayList = [];
        if (array.length == 0) {
            alert("购物车为空，不能结算！");
            return false;
        }
        let addressId = parseInt($("ins").prev().prev().find('#addressId').val());
        for (var i = 0; i < array.length; i++) {
            let productId = parseInt($(".productId").eq(i).val());
            let quantity = parseInt($(".car_ipt").eq(i).val());
            // let cost = parseInt($(".qprice").eq(i).html().substring(1));
            var cartList = {"productId": productId, "quantity": quantity, "addressId": addressId}
            arrayList.push(cartList);
        }
        var jsonList = JSON.stringify(arrayList);
        $.ajax({
            async: false,//true:异步，false:同步
            url: "/wood/order/add",
            type: "POST",
            datatype: "json",
            contentType: "application/json; charset=utf-8",
            // data: {'list': jsonList},
            data: jsonList,
            // traditional:true,//用传统方式序列化数据
            success: function (flag) {
                if (flag == "success") {
                    window.location.href = "/wood/order/orderList";
                }
            }

        });
        /*window.location.href = "/wood/order/pay";*/
    });
});

function addQuantity(obj) {
    let index = $(".car_btn_2").index(obj);
    let quantity = parseInt($(".car_ipt").eq(index).val());
    let stock = parseInt($(".productStock").eq(index).val());
    if (quantity == stock) {
        alert("库存不足！");
        return false;
    }
    quantity++;
    let price = parseFloat($(".productPrice").eq(index).val());
    let cost = quantity * price;
    let id = parseInt($(".cartId").eq(index).val());

    /* console.log("cost"+cost);
     console.log("price"+price);
     console.log("id"+id);*/
    $.ajax({
        url: "/wood/cart/update/" + id + "/" + quantity + "/" + cost,
        type: "POST",
        success: function (data) {
            if (data == "success") {
                $(".qprice").eq(index).text('￥' + cost);
                $(".car_ipt").eq(index).val(quantity);

                let array = $(".qprice");
                let totalCost = 0;
                for (let i = 0; i < array.length; i++) {
                    let val = parseInt($(".qprice").eq(i).html().substring(1));
                    totalCost += val;
                }
                // console.log("totalCost"+totalCost);
                $("#totalprice").html("￥" + totalCost);
            }
        }
    });
}

function subQuantity(obj) {
    let index = $(".car_btn_1").index(obj);
    let quantity = parseInt($(".car_ipt").eq(index).val());
    if (quantity == 1) {
        alert("至少选择一件商品！");
        return false;
    }
    quantity--;
    let price = parseFloat($(".productPrice").eq(index).val());
    let cost = quantity * price
    let id = parseInt($(".cartId").eq(index).val());
    $.ajax({
        url: "/wood/cart/update/" + id + "/" + quantity + "/" + cost,
        type: "POST",
        success: function (data) {
            if (data == "success") {
                $(".qprice").eq(index).text('￥' + cost);
                $(".car_ipt").eq(index).val(quantity);

                let array = $(".qprice");
                let totalCost = 0;
                for (let i = 0; i < array.length; i++) {
                    let val = parseInt($(".qprice").eq(i).html().substring(1));
                    totalCost += val;
                }
                // console.log("totalCost"+totalCost);
                $("#totalprice").html("￥" + totalCost);
            }
        }
    });
}

function removeCart(obj) {
    let index = $(".delete").index(obj);
    let id = parseInt($(".cartId").eq(index).val());
    if (confirm("是否确定删除?")) {
        window.location.href = "/wood/cart/deleteById/" + id;
    }
}

function settlement2() {
    var totalCost = $("#totalprice").text();
    if (totalCost == "￥0") {
        alert("购物车为空，不能结算！");
        return false;
    }
    window.location.href = "/cart/settlement2";
}

//删除地址
function delClick(obj) {
    let index = $(".delete-address").index(obj);
    let id = parseInt($(".id").eq(index).val());
    if (confirm("是否确定删除?")) {
        window.location.href = "/wood/cart/deleteAddress/" + id;
    }
}

//设置默认地址
function setDefault(obj) {
    let index = $(".setDefault").index(obj);
    let id = parseInt($(".id").eq(index).val());
    // console.log(".setDefault--" + index);
    // console.log("id--" + id);
    $(obj).parents("li").addClass("defaultAddr").siblings().removeClass("defaultAddr");
    window.location.href = "/wood/cart/defaultAddress/" + id;

}

